<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Model_pencarian extends CI_Model {
	
	var $table_name = array("Jadwal" => "jadwal_perjalanan", "Stasiun" => "stasiun", "Kereta" => "kereta");
	
	function __construct()
	{
		parent::__construct();
	}
	
	function cari($awal,$akhir,$waktuawal,$waktuakhir='')
	{
		if($waktuakhir==='')
		{
			$sql = 'SELECT a.no_krl, jam_ber, jam_dat, Kelas, id
					FROM (
						SELECT no_krl, jam_ber
						FROM '.$this->table_name['Jadwal'].' a,'.$this->table_name['Stasiun'].' b
						WHERE a.jam_ber > \''.$waktuawal.'\' AND a.kd_sta = b.Kode AND b.id = \''.$awal.'\'
						GROUP BY no_krl
					) a, (
						SELECT no_krl, jam_dat
						FROM '.$this->table_name['Jadwal'].' a,'.$this->table_name['Stasiun'].' b
						WHERE a.kd_sta_next = b.Kode AND b.id = \''.$akhir.'\'
						GROUP BY no_krl
					) b, '.$this->table_name['Kereta'].' c
					WHERE a.no_krl = b.no_krl AND c.No_KRL = a.no_krl
					GROUP BY a.no_krl';
			$query = $this->db->query($sql,array($awal,$akhir,$waktuawal));
		}
		else
		{
			
			$sql = 'SELECT a.no_krl, jam_ber, jam_dat, Kelas, id
					FROM (
						SELECT no_krl, jam_ber
						FROM '.$this->table_name['Jadwal'].' a,'.$this->table_name['Stasiun'].' b
						WHERE a.jam_ber > \''.$waktuawal.'\' AND a.kd_sta = b.Kode AND b.id = \''.$awal.'\'
						GROUP BY no_krl
					) a, (
						SELECT no_krl, jam_dat
						FROM '.$this->table_name['Jadwal'].' a,'.$this->table_name['Stasiun'].' b
						WHERE a.jam_dat < \''.$waktuakhir.'\' AND a.kd_sta_next = b.Kode AND b.id = \''.$akhir.'\'
						GROUP BY no_krl
					) b, '.$this->table_name['Kereta'].' c
					WHERE a.no_krl = b.no_krl  AND c.No_KRL = a.no_krl
					GROUP BY a.no_krl';
			$query = $this->db->query($sql,array($awal,$akhir,$waktuawal));
		}
		return $query->result();
	}
	
}
